package cate.game.admin.menu.mail;

import cate.common.table.d.GDMail;
import cate.game.GameBody;
import cate.game.admin.menu.MenuSetup;
import cate.game.mail.po.MailBuilder;
import cate.game.res.MixRes;
import cate.game.role.mail.Mail;
import cate.game.role.mail.msg.MailMyResp;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MailMenu extends MenuSetup {
    public MailMenu(GameBody gameBody) {
        super("邮件");
        reg(gameBody,
                "发送模板邮件",
                3,
                "【参数1】邮件ID,【参数2】邮件参数（用#分隔）,【参数3】奖励",
                (role, params) -> {
                    int contentId = toInt(params, 0, 20001);
                    List<Object> paramList = null;
                    String paramStr = toString(params, 1, null);

                    if (StringUtils.isNotBlank(paramStr)) {
                        String[] split = paramStr.split("#");
                        paramList = new ArrayList<>(Arrays.asList(split));
                    }
                    MixRes reward = null;
                    String rewardStr = toString(params, 2, null);
                    if (StringUtils.isNotBlank(rewardStr)) {
                        reward = new MixRes();
                        reward.parse(rewardStr);
                    }

                    Mail m = new MailBuilder()
                                    .setContentId(contentId)
                                    .setParamList(paramList)
                                    .setReward(reward)
                                    .build();
                    role.getMail().add(m);
                    gameBody.notice.message(role, "已添加了一封邮件!");
                });
        reg(gameBody,
                "发送自定义邮件",
                3,
                "【参数1】邮件标题,【参数2】邮件内容,【参数3】奖励",
                (role, params) -> {
                    MixRes reward = null;
                    String rewardStr = toString(params, 2, null);
                    if (StringUtils.isNotBlank(rewardStr)) {
                        reward = new MixRes();
                        reward.parse(rewardStr);
                    }

                    Mail m = new MailBuilder()
                            .setContent(toString(params, 1, null))
                            .setTitle(toString(params, 0, null))
                            .setReward(reward)
                            .build();
                    role.getMail().add(m);
                    gameBody.notice.message(role, "已添加了一封邮件!");
                });

        reg(gameBody,
                "清空邮件",
                0,
                "",
                (role, params) -> {
                    role.getMail().clearAll();
                    role.sendNow(new MailMyResp(role));
                    gameBody.notice.message(role, "已经清空邮件");
                });

        reg(gameBody,
                "塞满邮件",
                0,
                "",
                (role, params) -> {
                    int current = role.getMail().list.size();
                    for (int i = current; i < GDMail.MAX_NUM; i++) {
                        Mail m = new MailBuilder()
                                .setContent("塞满")
                                .setTitle("塞满")
                                .setReward(new MixRes("0:1:1"))
                                .build();
                        role.getMail().add(m);
                    }
                });
    }
}
